package com.ptswitch.material.pages.contract;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.util.DateFormatUtil;
import org.slf4j.Logger;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractItemService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.ContractUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class AddSpecialItem extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractItemService contractItemSer;
    @Inject
    private MaterialService materialSer;
    @Property
    private Contract contract;
    private long id;
    @Property
    private ContractItem contractItem;
    //
    @InjectPage
    private ViewContract viewContract;

    Object withInit(long id) {
        this.id = id;
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        withInit(id);
        contract = contractSer.findById(id);
        checkPermitted(PermissionUtil.ContractItemAdd);
        if (contract.isContractOk()) {
            setMessage("合同已完成");
            return viewContract.withInit(contract.getId());
        }
        contractItem = new ContractItem();
        String code = DateFormatUtil.format(new Date(), "'S'-DDD");
        code = contractItemSer.emptyCode(code);
        contractItem.setCode(code);
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { id };
    }

    @SetupRender
    Object display() {
        return null;
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "addItem")
    Object addItem() {
        checkPermitted(PermissionUtil.ContractItemAdd);
        contractItem.setContract(contract);
        //check code
        if (contractItemSer.existContractItemCode(contractItem)) {
            setMessage("合同中已有该物料编码");
            return null;
        }
        contractItem.computePrice();
        contractItemSer.save(contractItem);
        // update total price
        contract = contractSer.findById(id, ContractService.Init_Items);
        contract.updateForAddItem(contractItem);
        contractSer.save(contract);
//		//send email
//		EMail email = EMailUtil.newEMail();
//		email.setSubject(contract.getCreater().getRealname() + "添加了新的物料项");
//		email.setTosSplit(contract.getFinancial().getEmail() + ";" + contract.getProduction().getEmail());
//		String message = getWebUrl() + "/contract/view/" + contract.getId();
//		email.addContentText(message + "\n");
//		EMailUtil.send(email);
        return viewContract.withInit(id);
    }

    @Override
    protected List<Role> appendRoleToVisitor(Uzer visitor) {
        return ContractUtil.appendThisRole(contract, visitor);
    }
}
